package com.seraph.zys.util;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.beanutils.PropertyUtils;

import com.seraph.zys.dao.domain.PcPatientVisitFee;

public class UtilTools {
	
	public static String beanName2TableName(Class obj){
		String beanName=obj.getName();
		if(beanName.lastIndexOf(".")!=-1){
			beanName=beanName.substring(beanName.lastIndexOf(".")+1);
		}
		char[] ca=beanName.toCharArray();
		StringBuffer sb=new StringBuffer();
		for(int i =0;i<ca.length;i++){
			if(Character.isUpperCase(ca[i])){
				sb.append("_");
			}
			sb.append(ca[i]);
		}
		return sb.subSequence(1, sb.length()).toString();
	}
	
	public static String columnName2PropertyName(String colName){
		StringBuffer sb=new StringBuffer();
		if(colName!=null && colName.length()>0){
			char[] cr=colName.toCharArray();
			for(int i=0;i<cr.length;i++){
				if(cr[i]=='_'){
					if(++i>=cr.length){
						sb.append(cr[i]);
						break;
					}else{
						sb.append(String.valueOf(cr[i]).toUpperCase());
						continue;
					}
				}else
					sb.append(cr[i]);
			}
		}
		if(sb.length()>0){
			return sb.toString();
		}else
			return null;
	}
	
	public static Object getValueByPropertyType(Object obj,String propertyName,String propertyValue) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{
		Object returnObj=null;
		Class propertyClass=PropertyUtils.getPropertyType(obj,propertyName);
		if(Integer.class==propertyClass){
			returnObj=Double.valueOf(propertyValue).intValue();
		}else if(Double.class==propertyClass){
			returnObj=Double.valueOf(propertyValue);
		}else if(Long.class==propertyClass){
			returnObj=Double.valueOf(propertyValue).longValue();
		}else if(BigDecimal.class==propertyClass){
			returnObj=BigDecimal.valueOf(Double.valueOf(propertyValue).doubleValue());
		}
		return returnObj;
	}
	
	public static double getPropertyValueStringByColumnName(Object obj,String columnName) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{
		Object valueObj=PropertyUtils.getProperty(obj,columnName2PropertyName(columnName));
		if(valueObj!=null){
			return Double.valueOf(valueObj.toString());
		}else
			return 0d;
	}
	public static String getPropertyValueStrByColumnName(Object obj,String columnName) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{
		Object valueObj=PropertyUtils.getProperty(obj,columnName2PropertyName(columnName));
		if(valueObj!=null){
			return String.valueOf(valueObj.toString());
		}else
			return "";
	}
	public static String getSQLString(Object obj,String sql) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{
		Pattern pattern = Pattern.compile("\\$\\{\\s*([\\w]+)\\s*\\}");
		Matcher matcher = pattern.matcher(sql);
		String varStr=null;
		String properyValue=null;
		while(matcher.find()){
			varStr=matcher.group(1);
			if(varStr!=null && varStr.trim().length()>0){
				properyValue=getPropertyValueStrByColumnName(obj,varStr);
				sql=sql.replaceAll("\\$\\{\\s*"+varStr+"\\s*\\}", properyValue);
			}
			properyValue=null;
			varStr=null;
		}
		
		return sql;
	}
	public static void main(String[] args){
		PcPatientVisitFee pcPatientVisitFee=new PcPatientVisitFee();
		try {
			System.out.println(getValueByPropertyType(pcPatientVisitFee,"total","123"));
//			System.out.println(PropertyUtils.getProperty(pcPatientVisitFee,"total").toString());
			Integer i=null;
			System.out.println(i);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
